<?php

namespace backend\modules\proxy_pay\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\proxy_pay\models\ProxyPayBack;

/**
 * ProxyPayBackSearch represents the model behind the search form about `backend\modules\proxy_pay\models\ProxyPayBack`.
 */
class ProxyPayBackSearch extends ProxyPayBack
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'proxy_pay_id', 'state', 'original_created_at', 'back_time', 'amount'], 'integer'],
            [['result', 'created_at', 'reason', 'fuiou_order_no', 'back_order_no', 'accnt_no', 'accnt_nm', 'bank_no', 'other_data'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ProxyPayBack::find()->orderBy(['id'=>SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'proxy_pay_id' => $this->proxy_pay_id,
//            'created_at' => $this->created_at,
            'state' => $this->state,
            'original_created_at' => $this->original_created_at,
            'back_time' => $this->back_time,
            'amount' => $this->amount,
        ]);

        $query->andFilterWhere(['like', 'result', $this->result])
            ->andFilterWhere(['like', 'reason', $this->reason])
            ->andFilterWhere(['like', 'fuiou_order_no', $this->fuiou_order_no])
            ->andFilterWhere(['like', 'back_order_no', $this->back_order_no])
            ->andFilterWhere(['like', 'accnt_no', $this->accnt_no])
            ->andFilterWhere(['like', 'accnt_nm', $this->accnt_nm])
            ->andFilterWhere(['like', 'bank_no', $this->bank_no])
            ->andFilterWhere(['like', 'other_data', $this->other_data]);

        if ( ! is_null($this->created_at) && strpos($this->created_at, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->created_at);
            $query->andFilterWhere(['between', 'created_at', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }
        /*if ( ! is_null($this->original_created_at) && strpos($this->original_created_at, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->original_created_at);
            $query->andFilterWhere(['between', 'original_created_at', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }
        if ( ! is_null($this->back_time) && strpos($this->back_time, ' - ') !== false ) {
            list($start_date, $end_date) = explode(' - ', $this->back_time);
            $query->andFilterWhere(['between', 'back_time', strtotime($start_date), strtotime($end_date.' 23:59:59')]);
        }*/
        return $dataProvider;
    }
}
